#include "IPListXml.h"

IPListXml::IPListXml()
{
}

bool IPListXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "Net")   //解析NET表
        {
            QDomNode netChild = rootChild.toElement().firstChild();
            PortStruct net;
            while(!netChild.isNull())
            {
                if (netChild.toElement().tagName() == "IP")  //解析IP
                {
                    QString ip = netChild.toElement().text();
                    net.setIp(ip);
                }
                if(netChild.toElement().tagName() == "Port")
                {
                    quint16 port = netChild.toElement().text().toUInt();
                    net.setPort(port);
                }
                netChild = netChild.nextSibling();
            }
            m_netList.append(net);
        }
        rootChild = rootChild.nextSibling();
    }
    return true;

}

bool IPListXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_netList.clear();
    return true;
}

QList<PortStruct> IPListXml::netList() const
{
    return m_netList;
}

void IPListXml::setNetList(const QList<PortStruct> &netList)
{
    m_netList = netList;
}
